<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveSupport::Subscriber</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Class</span> 
            ActiveSupport::Subscriber 
            
                <span class="parent">&lt; 
                    
                    <a href="../Object.html">Object</a>
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activesupport/lib/active_support/subscriber_rb.html">activesupport/lib/active_support/subscriber.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p><a href="Subscriber.html">ActiveSupport::Subscriber</a> is an object set to
consume <a href="Notifications.html">ActiveSupport::Notifications</a>. The
subscriber dispatches notifications to a registered object based on its
given namespace.</p>

<p>An example would be Active Record subscriber responsible for collecting
statistics about queries:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">ActiveRecord</span>
  <span class="ruby-keyword">class</span> <span class="ruby-constant">StatsSubscriber</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Subscriber</span>
    <span class="ruby-keyword">def</span> <span class="ruby-identifier">sql</span>(<span class="ruby-identifier">event</span>)
      <span class="ruby-constant">Statsd</span>.<span class="ruby-identifier">timing</span>(<span class="ruby-node">&quot;sql.#{event.payload[:name]}&quot;</span>, <span class="ruby-identifier">event</span>.<span class="ruby-identifier">duration</span>)
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>And it’s finally registered as:</p>

<pre>ActiveRecord::StatsSubscriber.attach_to :active_record</pre>

<p>Since we need to know all instance methods before attaching the log
subscriber, the line above should be called after your subscriber
definition.</p>

<p>After configured, whenever a “sql.active_record” notification is published,
it will properly dispatch the event (<a
href="Notifications/Event.html">ActiveSupport::Notifications::Event</a>) to
the <code>sql</code> method.</p>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>A</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Subscriber.html#method-c-attach_to">attach_to</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>F</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Subscriber.html#method-i-finish">finish</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>N</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Subscriber.html#method-c-new">new</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Subscriber.html#method-i-start">start</a>,
              </li>
            
              
              <li>
                <a href="Subscriber.html#method-c-subscribers">subscribers</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
    
      <div class="sectiontitle">Class Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-c-attach_to">
            
              <b>attach_to</b>(namespace, subscriber=new, notifier=ActiveSupport::Notifications)
            
            <a href="Subscriber.html#method-c-attach_to" name="method-c-attach_to" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Attach the subscriber to a namespace.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-attach_to_source')" id="l_method-c-attach_to_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/be7e34bc1ef81a5c78a86be7201d3aab5106978b/activesupport/lib/active_support/subscriber.rb#L33" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-attach_to_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/subscriber.rb, line 33</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">attach_to</span>(<span class="ruby-identifier">namespace</span>, <span class="ruby-identifier">subscriber</span>=<span class="ruby-identifier">new</span>, <span class="ruby-identifier">notifier</span>=<span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span>)
  <span class="ruby-identifier">subscribers</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">subscriber</span>

  <span class="ruby-identifier">subscriber</span>.<span class="ruby-identifier">public_methods</span>(<span class="ruby-keyword">false</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">event</span><span class="ruby-operator">|</span>
    <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-node">%w{ start finish }</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">event</span>.<span class="ruby-identifier">to_s</span>)

    <span class="ruby-identifier">notifier</span>.<span class="ruby-identifier">subscribe</span>(<span class="ruby-node">&quot;#{event}.#{namespace}&quot;</span>, <span class="ruby-identifier">subscriber</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-new">
            
              <b>new</b>()
            
            <a href="Subscriber.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/be7e34bc1ef81a5c78a86be7201d3aab5106978b/activesupport/lib/active_support/subscriber.rb#L48" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/subscriber.rb, line 48</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>
  <span class="ruby-ivar">@queue_key</span> = [<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">name</span>, <span class="ruby-identifier">object_id</span>].<span class="ruby-identifier">join</span> <span class="ruby-string">&quot;-&quot;</span>
  <span class="ruby-keyword">super</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-subscribers">
            
              <b>subscribers</b>()
            
            <a href="Subscriber.html#method-c-subscribers" name="method-c-subscribers" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-subscribers_source')" id="l_method-c-subscribers_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/be7e34bc1ef81a5c78a86be7201d3aab5106978b/activesupport/lib/active_support/subscriber.rb#L43" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-subscribers_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/subscriber.rb, line 43</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">subscribers</span>
  <span class="ruby-identifier">@@subscribers</span> <span class="ruby-operator">||=</span> []
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                  
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-finish">
            
              <b>finish</b>(name, id, payload)
            
            <a href="Subscriber.html#method-i-finish" name="method-i-finish" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-finish_source')" id="l_method-i-finish_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/be7e34bc1ef81a5c78a86be7201d3aab5106978b/activesupport/lib/active_support/subscriber.rb#L61" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-finish_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/subscriber.rb, line 61</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">finish</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">id</span>, <span class="ruby-identifier">payload</span>)
  <span class="ruby-identifier">finished</span>  = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>
  <span class="ruby-identifier">event</span>     = <span class="ruby-identifier">event_stack</span>.<span class="ruby-identifier">pop</span>
  <span class="ruby-identifier">event</span>.<span class="ruby-identifier">end</span> = <span class="ruby-identifier">finished</span>
  <span class="ruby-identifier">event</span>.<span class="ruby-identifier">payload</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">payload</span>)

  <span class="ruby-identifier">method</span> = <span class="ruby-identifier">name</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">'.'</span>).<span class="ruby-identifier">first</span>
  <span class="ruby-identifier">send</span>(<span class="ruby-identifier">method</span>, <span class="ruby-identifier">event</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-start">
            
              <b>start</b>(name, id, payload)
            
            <a href="Subscriber.html#method-i-start" name="method-i-start" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-start_source')" id="l_method-i-start_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/be7e34bc1ef81a5c78a86be7201d3aab5106978b/activesupport/lib/active_support/subscriber.rb#L53" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-start_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/subscriber.rb, line 53</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">start</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">id</span>, <span class="ruby-identifier">payload</span>)
  <span class="ruby-identifier">e</span> = <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Notifications</span><span class="ruby-operator">::</span><span class="ruby-constant">Event</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span>, <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">id</span>, <span class="ruby-identifier">payload</span>)
  <span class="ruby-identifier">parent</span> = <span class="ruby-identifier">event_stack</span>.<span class="ruby-identifier">last</span>
  <span class="ruby-identifier">parent</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">e</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">parent</span>

  <span class="ruby-identifier">event_stack</span>.<span class="ruby-identifier">push</span> <span class="ruby-identifier">e</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    